package com.thinkwide.data.design;

import com.thinkwide.data.exchange.MFXml;
import com.thinkwide.data.core.MFString;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MFDataForm {
    private MFFormType formtype;
    private List<MFDataFormItem> forms;

    public MFDataForm() {
        setForms(new ArrayList<MFDataFormItem>());
    }

    public void loadData(String formid) {
        String data = "";
        Document xml = MFXml.load("F:\\Weaver\\picc-work\\test.txt");
        data = xml.asXML();
        Document doc = MFXml.string2Document(data);
        Element root = doc.getRootElement();
        Element element = root.element("formtype");
        if (element != null) {
            String str = element.getText();
            this.formtype = MFFormType.valueOf(str);

        }

        //load form
        for (Iterator<?> i = root.elementIterator("form"); i.hasNext(); ) {
            Element formItem = (Element) i.next();
            MFDataFormItem form = new MFDataFormItem();
            forms.add(form);

            form.setItemType(MFXml.getAttrValue(formItem, "type"));
            form.setTitle(MFXml.getAttrValue(formItem, "title"));

            Element dataItem = formItem.element("primaryFields");
            form.setPrimaryFields(dataItem.getText());

            dataItem = formItem.element("tableName");
            form.setTableName(dataItem.getText());

            dataItem = formItem.element("autoUpdateFields");
            if (dataItem != null)
                if (MFString.notNullOrEmpty(dataItem.getText()))
                    form.setAutoUpdateFields(Boolean.valueOf(dataItem.getText()));

            dataItem = formItem.element("insertFields");
            form.setInsertFields(dataItem.getText());

            dataItem = formItem.element("modifyFields");
            form.setModifyFields(dataItem.getText());

            dataItem = formItem.element("relation");
            MFDataRelation relation = new MFDataRelation();

            relation.setMainfield(MFXml.getAttrValue(dataItem, "mainField"));
            relation.setDetailfield(MFXml.getAttrValue(dataItem, "detailField"));


            Element toolbar = formItem.element("toolbar");
            if (toolbar != null) {
                for (Iterator<?> j = toolbar.elementIterator("item"); j.hasNext(); ) {
                    Element toolbarItem = (Element) j.next();
                    MFDataBarItem barItem = new MFDataBarItem();
                    form.getToolbar().add(barItem);

                    barItem.setButtonType(MFXml.getAttrValue(toolbarItem, "type"));
                    barItem.setEnable(MFXml.getAttrValue(toolbarItem, "enable"));
                    barItem.setField(MFXml.getAttrValue(toolbarItem, "field"));
                    barItem.setIcon(MFXml.getAttrValue(toolbarItem, "icon"));
                    barItem.setJs(MFXml.getAttrValue(toolbarItem, "js"));
                    barItem.setTitle(MFXml.getAttrValue(toolbarItem, "title"));
                    barItem.setVisible(MFXml.getAttrValue(toolbarItem, "visible"));

                }
            }

            Element search = formItem.element("search");
            if (search != null) {
                for (Iterator<?> j = search.elementIterator("item"); j.hasNext(); ) {
                    Element searchItem = (Element) j.next();
                    MFDataFieldItem fieldItem = new MFDataFieldItem();
                    form.getSearch().add(fieldItem);

                    fieldItem.setEnable(MFXml.getAttrValue(searchItem, "enable"));
                    fieldItem.setField(MFXml.getAttrValue(searchItem, "field"));
                    fieldItem.setJs(MFXml.getAttrValue(searchItem, "js"));
                    fieldItem.setTitle(MFXml.getAttrValue(searchItem, "title"));
                    fieldItem.setVisible(MFXml.getAttrValue(searchItem, "visible"));
                    fieldItem.setControl(MFXml.getAttrValue(searchItem, "control"));
                    fieldItem.setDefValue(MFXml.getAttrValue(searchItem, "defValue"));
                    fieldItem.setHeight(MFXml.getAttrValue(searchItem, "height"));
                    fieldItem.setWidth(MFXml.getAttrValue(searchItem, "width"));
                    fieldItem.setNotnull(MFXml.getAttrValue(searchItem, "notnull"));
                    fieldItem.setLookupid(MFXml.getAttrValue(searchItem, "lookupid"));
                    fieldItem.setHint(MFXml.getAttrValue(searchItem, "hint"));
                    fieldItem.setHelpId(MFXml.getAttrValue(searchItem, "helpId"));

                }
            }

            Element grid = formItem.element("grid");
            if (grid != null) {
                for (Iterator<?> j = grid.elementIterator("item"); j.hasNext(); ) {
                    Element gridItem = (Element) j.next();
                    MFDataFieldItem fieldItem = new MFDataFieldItem();
                    form.getGrid().add(fieldItem);

                    fieldItem.setEnable(MFXml.getAttrValue(gridItem, "enable"));
                    fieldItem.setField(MFXml.getAttrValue(gridItem, "field"));
                    fieldItem.setJs(MFXml.getAttrValue(gridItem, "js"));
                    fieldItem.setTitle(MFXml.getAttrValue(gridItem, "title"));
                    fieldItem.setVisible(MFXml.getAttrValue(gridItem, "visible"));
                    fieldItem.setControl(MFXml.getAttrValue(gridItem, "control"));
                    fieldItem.setDefValue(MFXml.getAttrValue(gridItem, "defValue"));
                    fieldItem.setHeight(MFXml.getAttrValue(gridItem, "height"));
                    fieldItem.setWidth(MFXml.getAttrValue(gridItem, "width"));
                    fieldItem.setNotnull(MFXml.getAttrValue(gridItem, "notnull"));
                    fieldItem.setLookupid(MFXml.getAttrValue(gridItem, "lookupid"));
                    fieldItem.setHint(MFXml.getAttrValue(gridItem, "hint"));
                    fieldItem.setHelpId(MFXml.getAttrValue(gridItem, "helpId"));

                }
            }


            Element edit = formItem.element("edit");
            if (edit != null) {
                for (Iterator<?> j = edit.elementIterator("item"); j.hasNext(); ) {
                    Element editItem = (Element) j.next();
                    MFDataFieldItem fieldItem = new MFDataFieldItem();
                    form.getEdit().add(fieldItem);

                    fieldItem.setEnable(MFXml.getAttrValue(editItem, "enable"));
                    fieldItem.setField(MFXml.getAttrValue(editItem, "field"));
                    fieldItem.setJs(MFXml.getAttrValue(editItem, "js"));
                    fieldItem.setTitle(MFXml.getAttrValue(editItem, "title"));
                    fieldItem.setVisible(MFXml.getAttrValue(editItem, "visible"));
                    fieldItem.setControl(MFXml.getAttrValue(editItem, "control"));
                    fieldItem.setDefValue(MFXml.getAttrValue(editItem, "defValue"));
                    fieldItem.setHeight(MFXml.getAttrValue(editItem, "height"));
                    fieldItem.setWidth(MFXml.getAttrValue(editItem, "width"));
                    fieldItem.setNotnull(MFXml.getAttrValue(editItem, "notnull"));
                    fieldItem.setLookupid(MFXml.getAttrValue(editItem, "lookupid"));
                    fieldItem.setHint(MFXml.getAttrValue(editItem, "hint"));
                    fieldItem.setHelpId(MFXml.getAttrValue(editItem, "helpId"));

                }
            }

        }

    }

    public void saveData(String formid) {
        String data = "";

        Document doc = DocumentHelper.createDocument();
        Element root = doc.addElement("root");
        Element element = root.addElement("formtype");
        element.setText(this.formtype.toString());

        //load form
        for (MFDataFormItem form : forms) {
            Element formItem = root.addElement("form");

            formItem.addAttribute("type", form.getItemType());
            formItem.addAttribute("title", form.getTitle());

            Element dataItem = formItem.addElement("primaryFields");
            dataItem.setText(form.getPrimaryFields());

            dataItem = formItem.addElement("insertFields");
            dataItem.setText(form.getInsertFields());

            dataItem = formItem.addElement("modifyFields");
            dataItem.setText(form.getModifyFields());

            dataItem = formItem.addElement("tableName");
            dataItem.setText(form.getTableName());

            dataItem = formItem.addElement("autoUpdateFields");
            dataItem.setText(form.isAutoUpdateFields().toString());

            dataItem = formItem.addElement("relation");
            dataItem.addAttribute("mainField", form.getRelation().getMainfield());
            dataItem.addAttribute("detailField", form.getRelation().getDetailfield());

            Element toolbar = formItem.addElement("toolbar");

            for (MFDataBarItem barItem : form.getToolbar()) {
                Element toolbarItem = toolbar.addElement("item");
                toolbarItem.addAttribute("type", barItem.getButtonType());
                toolbarItem.addAttribute("enable", barItem.getEnable());
                toolbarItem.addAttribute("field", barItem.getField());
                toolbarItem.addAttribute("icon", barItem.getIcon());
                toolbarItem.addAttribute("js", barItem.getJs());
                toolbarItem.addAttribute("title", barItem.getTitle());
                toolbarItem.addAttribute("visible", barItem.getVisible());
            }

            Element search = formItem.addElement("search");

            for (MFDataFieldItem fieldItem : form.getSearch()) {
                Element searchItem = search.addElement("item");

                searchItem.addAttribute("enable", fieldItem.getEnable());
                searchItem.addAttribute("field", fieldItem.getField());
                searchItem.addAttribute("js", fieldItem.getJs());
                searchItem.addAttribute("title", fieldItem.getTitle());
                searchItem.addAttribute("visible", fieldItem.getVisible());
                searchItem.addAttribute("control", fieldItem.getEnable());
                searchItem.addAttribute("defValue", fieldItem.getDefValue());
                searchItem.addAttribute("height", fieldItem.getHeight());
                searchItem.addAttribute("width", fieldItem.getWidth());
                searchItem.addAttribute("notnull", fieldItem.getNotnull());
                searchItem.addAttribute("lookupid", fieldItem.getLookupid());
                searchItem.addAttribute("hint", fieldItem.getHint());
                searchItem.addAttribute("helpId", fieldItem.getHelpId());

            }

            Element grid = formItem.addElement("grid");
            for (MFDataFieldItem fieldItem : form.getGrid()) {

                Element gridItem = grid.addElement("item");

                gridItem.addAttribute("enable", fieldItem.getEnable());
                gridItem.addAttribute("field", fieldItem.getField());
                gridItem.addAttribute("js", fieldItem.getJs());
                gridItem.addAttribute("title", fieldItem.getTitle());
                gridItem.addAttribute("visible", fieldItem.getVisible());
                gridItem.addAttribute("control", fieldItem.getEnable());
                gridItem.addAttribute("defValue", fieldItem.getDefValue());
                gridItem.addAttribute("height", fieldItem.getHeight());
                gridItem.addAttribute("width", fieldItem.getWidth());
                gridItem.addAttribute("notnull", fieldItem.getNotnull());
                gridItem.addAttribute("lookupid", fieldItem.getLookupid());
                gridItem.addAttribute("hint", fieldItem.getHint());
                gridItem.addAttribute("helpId", fieldItem.getHelpId());
            }

            Element edit = formItem.addElement("edit");
            for (MFDataFieldItem fieldItem : form.getEdit()) {
                Element editItem = edit.addElement("item");
                editItem.addAttribute("enable", fieldItem.getEnable());
                editItem.addAttribute("field", fieldItem.getField());
                editItem.addAttribute("js", fieldItem.getJs());
                editItem.addAttribute("title", fieldItem.getTitle());
                editItem.addAttribute("visible", fieldItem.getVisible());
                editItem.addAttribute("control", fieldItem.getEnable());
                editItem.addAttribute("defValue", fieldItem.getDefValue());
                editItem.addAttribute("height", fieldItem.getHeight());
                editItem.addAttribute("width", fieldItem.getWidth());
                editItem.addAttribute("notnull", fieldItem.getNotnull());
                editItem.addAttribute("lookupid", fieldItem.getLookupid());
                editItem.addAttribute("hint", fieldItem.getHint());
                editItem.addAttribute("helpId", fieldItem.getHelpId());
                ;

            }

        }

        MFXml.doc2XmlFile(doc, "F:\\Weaver\\picc-work\\test1.txt");
    }

    public void setFormtype(MFFormType formtype) {
        this.formtype = formtype;
    }

    public MFFormType getFormtype() {
        return formtype;
    }

    public void setForms(List<MFDataFormItem> forms) {
        this.forms = forms;
    }

    public List<MFDataFormItem> getForms() {
        return forms;
    }
}
